﻿using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NoMvvm.Wpf;
using Templates.Models;
using Templates.ViewModels;

namespace Templates.UnitTests
{
    [TestClass]
    public class MainViewModelTest
    {
        private readonly TemplateFormatter _formatter = new TemplateFormatter();
        private readonly Serializer _serializer = new Serializer();
        private readonly Mocking.MockDialogService _dialogService = new Mocking.MockDialogService();
        private readonly IMessageBoxService _mbService = new Mocking.MockMessagBoxService();

        /// <summary>
        ///A test for ImportTemplate
        ///</summary>
        [TestMethod]
        public void ImportTemplateTest()
        {
            _dialogService.Path = TestConstants.TestImport;
            MainViewModel viewModel = new MainViewModel(_dialogService, _mbService, _formatter, _serializer);
            dynamic actual = viewModel;
            actual.StartCharacter = "[";
            actual.EndCharacter = "]";
            actual.IgnoreCase = true;
            viewModel.ImportTemplate();

            Assert.AreEqual(1, actual.Variables.Count);
            Assert.AreEqual("WOOT", actual.Variables[0].Name);
        }

        /// <summary>
        /// A test for AddVariable
        /// </summary>
        [TestMethod]
        public void AddVariableTest()
        {
            MainViewModel viewModel = new MainViewModel(_dialogService, _mbService, _formatter, _serializer);
            viewModel.AddVariable();
            dynamic actual = viewModel;

            Assert.AreEqual(1, actual.Variables.Count);
        }

        /// <summary>
        ///A test for OpenFile
        ///</summary>
        [TestMethod]
        public void OpenFileTest()
        {
            _dialogService.Path = TestConstants.TestData;
            MainViewModel viewModel = new MainViewModel(_dialogService, _mbService, _formatter, _serializer);
            viewModel.OpenFile();

            dynamic actual = viewModel;
            Assert.AreEqual("This is some test data!", actual.Body);
        }

        /// <summary>
        ///A test for OpenTemplate
        ///</summary>
        [TestMethod]
        public void OpenTemplateTest()
        {
            _dialogService.Path = TestConstants.TestTemplate;
            MainViewModel viewModel = new MainViewModel(_dialogService, _mbService, _formatter, _serializer);
            viewModel.OpenTemplate();

            dynamic actual = viewModel;
            Assert.AreEqual("[", actual.StartCharacter);
            Assert.AreEqual("]", actual.EndCharacter);
            Assert.AreEqual(true, actual.IgnoreCase);
            Assert.AreEqual(1, actual.Variables.Count);
            Assert.AreEqual("WOOT", actual.Variables[0].Name);
            Assert.AreEqual("Hello!", actual.Variables[0].Value);
        }

        /// <summary>
        ///A test for SaveFile
        ///</summary>
        [TestMethod]
        public void SaveFileTest()
        {
            _dialogService.Path = Path.GetTempFileName();
            try
            {
                MainViewModel viewModel = new MainViewModel(_dialogService, _mbService, _formatter, _serializer);
                dynamic actual = viewModel;

                actual.StartCharacter = "[";
                actual.EndCharacter = "]";
                actual.IgnoreCase = true;
                actual.Variables = new List<Variable>
                {
                    new Variable { Name = "WOOT", Value = "test" },
                };
                actual.Body = "This is a [WOOT]!";
                viewModel.SaveFile();

                Assert.AreEqual("This is a test!", File.ReadAllText(_dialogService.Path));
            }
            finally
            {
                File.Delete(_dialogService.Path);
            }
        }

        /// <summary>
        ///A test for SaveTemplate
        ///</summary>
        [TestMethod]
        public void SaveTemplateTest()
        {
            _dialogService.Path = Path.GetTempFileName();
            try
            {
                MainViewModel viewModel = new MainViewModel(_dialogService, _mbService, _formatter, _serializer);
                dynamic actual = viewModel;

                actual.StartCharacter = "[";
                actual.EndCharacter = "]";
                actual.IgnoreCase = true;
                actual.Variables = new List<Variable>
                {
                    new Variable { Name = "WOOT", Value = "Hello!" },
                };
                actual.Body = "This is a [WOOT]!";
                viewModel.SaveTemplate();

                Assert.AreEqual(File.ReadAllText(TestConstants.TestTemplate), File.ReadAllText(_dialogService.Path));
            }
            finally
            {
                File.Delete(_dialogService.Path);
            }
        }
    }
}
